package com.ftg.learn.demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发送端
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/5/31 下午3:12
 */
public class Sending {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("kjx");
        factory.setPassword("kjx");
        factory.setVirtualHost("/kjx");
        try (
                //jdbc  : connnection 连接
                //        statement 静态sql语句
                //        executeQuery

                //rabbitmq
                //   connection -> channel(通道) -> vhost(虚拟机) ->{ exchange(交换器) -> queue(队列) -> message}
                //nacos -> connectio(spring配置文件) -> namespace -> group -> dataId


                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel()
        ) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World 1111";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
        } catch (IOException | TimeoutException e) {
            throw new RuntimeException(e);
        }
    }

}
